package router

import (
	"net/http"
	"pids-cloud-server/app/api"
	"pids-cloud-server/app/middleware"
	"pids-cloud-server/docs"

	"github.com/gin-gonic/gin"
	swaggerfiles "github.com/swaggo/files"
	ginSwagger "github.com/swaggo/gin-swagger"
)

func Routers(router *gin.Engine) {
	// 添加日志中间件
	router.Use(middleware.RequestLogger())
	registerSwagger(router)
	// 添加跨域支持
	router.Use(func(c *gin.Context) {
		c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
		c.Writer.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
		c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization")
		if c.Request.Method == "OPTIONS" {
			c.AbortWithStatus(http.StatusNoContent)
			return
		}
		c.Next()
	})

	group := router.Group("/pidscloud")
	// 定义一个简单的路由
	router.GET("/", api.Index)

	group.POST("/alarmThreshold/updateThreshold", api.UpdateAlarmThreshold)

	// 告警状态相关路由
	group.GET("/alarm/status", api.GetAllAlarmStatus)       // 获取所有告警状态
	group.GET("/alarm/active", api.GetActiveAlarmHosts)     // 获取有活跃告警的主机
	group.GET("/alarm/inactive", api.GetInactiveAlarmHosts) // 获取没有活跃告警的主机

	group.POST("/agent/minTask", api.UploadDataTask)
	group.GET("/model", api.GetModelList)

	// SNMP相关路由
	group.GET("/snmp/list", middleware.Pagination(), api.GetSnmpList)
	group.POST("/snmp/add", api.AddSnmpInfo)
	group.PUT("/snmp/update", api.UpdateSnmpInfo)
	group.DELETE("/snmp/delete/:id", api.DeleteSnmpInfo)

	// 数据库 相关路由
	group.GET("/db/list", middleware.Pagination(), api.GetDbList)
	group.POST("/db/add", api.AddDbInfo)
	group.PUT("/db/update", api.UpdateDbInfo)
	group.DELETE("/db/delete/:id", api.DeleteDbInfo)

	// Redis 相关路由
	group.GET("/redis/list", middleware.Pagination(), api.GetRedisList)
	group.POST("/redis/add", api.AddRedisInfo)
	group.PUT("/redis/update", api.UpdateRedisInfo)
	group.DELETE("/redis/delete/:id", api.DeleteRedisInfo)
	//docker相关路由
	group.GET("/docker/list", middleware.Pagination(), api.GetDockerList)
	group.POST("/docker/add", api.AddDockerInfo)
	group.PUT("/docker/update", api.UpdateDockerInfo)
	group.DELETE("/docker/delete/:id", api.DeleteDockerInfo)

	// 接口相关路由
	group.GET("/interface/list", middleware.Pagination(), api.GetInterfaceList)
	group.POST("/interface/add", api.AddInterfaceInfo)
	group.PUT("/interface/update", api.UpdateInterfaceInfo)
	group.DELETE("/interface/delete/:id", api.DeleteInterfaceInfo)

	// 数据同步相关路由
	group.POST("/sync/snmp", api.SyncToSnmpInfo)
	group.GET("/sync/compare", api.CompareSnmpSync)

	// ModelOID相关路由
	group.GET("/modeloid/list", middleware.Pagination(), api.GetModelOidList)
	group.POST("/modeloid/add", api.AddModelOid)
	group.PUT("/modeloid/update", api.UpdateModelOid)
	group.DELETE("/modeloid/delete/:id", api.DeleteModelOid)
	group.GET("/modeloid/export", api.ExportModelOid)
	group.POST("/modeloid/import", api.ImportModelOid)
	group.GET("/modeloid/models", api.GetModelOidModels)
}

func registerSwagger(router *gin.Engine) {
	docs.SwaggerInfo.BasePath = "/pidscloud"
	docs.SwaggerInfo.Title = "snmp采集后台接口"
	docs.SwaggerInfo.Description = "实现一个snmp采集的后端API服务"
	docs.SwaggerInfo.Version = "1.0"
	docs.SwaggerInfo.Host = "localhost:9999"
	docs.SwaggerInfo.Schemes = []string{"http", "https"}
	router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerfiles.Handler))
}
